// pages/user/index.js
const app = getApp()

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');
let util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    login: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if(!userInfo) {
      this.setData({login: false})
    }else {
      this.setData({ login: true, userInfo: userInfo })
    }
  },
  getInfo(e) {
    var _this = this
    if (e.detail.errMsg.search('ok') != -1) {
      var userInfo = e.detail.userInfo
      
      wx.showLoading({
        title: '加载中，请稍候',
      })

      //调用登录接口
      app.login(userInfo, function (res) {
        var login_res = res.data;

        if (login_res.code != 1 && login_res.code != 0) {     //登陆失败
          wx.hideLoading();
          wx.showToast({
            title: '登录失败...',
            icon: 'none',
            duration: 2000
          })
        } else {                                              //登陆成功
          wx.setStorageSync('user_token', login_res.user_info.user_token);
          wx.setStorageSync('userInfo', login_res.user_info);

          wx.hideLoading();
          wx.showToast({
            title: '登录成功',
            icon: 'none'
          })
          _this.setData({ login: true, userInfo: login_res.user_info })

          req.request(api.getUserEditableInfo, { data: { token: login_res.user_info.user_token } }, function (res) {
            res = res.data
            let custom = res;
            if (custom) {
              custom.country = custom.country == '' ? 'Andorra' : custom.country
              custom.code = custom.code == '' ? 'AD' : custom.code
              custom.firstName = custom.firstname
              custom.fullName = custom.fullname
              custom.lastName = custom.lastname
              custom.token = login_res.user_info.user_token
              delete (custom.firstname)
              delete (custom.fullname)
              delete (custom.lastname);

              wx.setStorageSync('custom', custom);
            }
          })
        }
      });

    } else {
      wx.showToast({
        title: '您需要同意我们获取您的信息才能继续使用',
        icon: 'none',
        duration: 3000
      })
    }
  },
  toOrder(){
    if (!wx.getStorageSync('user_token')) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/user/indent',
    })
  },
  toCommon() {
    if(!wx.getStorageSync('user_token')) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/user/info',
    })
  },
  logout() {
    wx.clearStorageSync();
    this.setData({userInfo: {}, login: false});
    wx.showToast({
      title: '已退出登录',
      icon: 'none'
    })
  }
})